package com.sinodata.bsm.cicp.collector.environment;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * 
 * @author 友联创新 hehy  
 * @date Dec 22, 2008
 */
public class EmersonCMplus {
    public static final int baudRate = 1200;
    public static final int dataBits = 8;
    public static final int stopBits = 1;
    public static final int parity = 0;
    public static Logger logger = Logger.getLogger(EmersonCMplus.class);

    public static byte getAdr(int pn) {
        //(机组编号－1) XOR F0h
        return (byte) ((pn - 1) ^ 0xF0);
    }

    public static byte calcChksum(byte[] bytes) {
        int sum = 0;
        for (int i = 0; i < bytes.length - 1; i++) {
            sum += bytes[i];
        }
        return (byte) (sum & 0xFF);
    }

    public static int combineBCD(byte high, byte low) {
        int a = (high >> 4) & 0xF;
        int b = high & 0xF;
        int c = (low >> 4) & 0xF;
        int d = low & 0xF;
        return a * 1000 + b * 100 + c * 10 + d;
    }

    //查询命令
    public static byte[] queryCmd(int pn) {
        byte[] cmd = new byte[] { getAdr(pn), 0x05, 0x2, 0x00, 0x0 };
        cmd[4] = calcChksum(cmd);
        return cmd;
    }

    /**
     * 从查询返回中解析出开关变量
     * 位	描述
     * 0	机组开
     * 1	报警蜂鸣器开
     * 7	1,数据冷却器机组;0,LECS15机组
     *  2~6	未定义
     * @param resp
     * @return
     */
    public static Byte getSwitchInQueryResp(byte[] resp) {
        if (calcChksum(resp) != resp[resp.length - 1]) {
            logger.error("校验和不正确");
            return null;
        }
        return resp[3];
    }

    /**
     * 温/湿度、温/湿度设定值
     * @param resp
     * @return
     */
    public static Integer[] getTHInQueryResp(byte[] resp) {
        if (calcChksum(resp) != resp[resp.length - 1]) {
            logger.error("校验和不正确");
            return null;
        }
        int t = combineBCD(resp[4], resp[5]);
        int h = combineBCD(resp[6], resp[7]);
        //T=17+((字节12) AND F0h)/16 C
        //H=42+((字节12) AND0Fh)*2% rh
        int tSet = 17 + (resp[11] & 0xF0) / 16;
        int hSet = 42 + (resp[11] & 0xF) * 2;
        return new Integer[] { t, h, tSet, hSet };
    }

    /**
     * 运行时间
     * @param resp
     * @return
     */
    public static Integer[] getRunTimeInQueryResp(byte[] resp) {
        if (calcChksum(resp) != resp[resp.length - 1]) {
            logger.error("校验和不正确");
            return null;
        }
        //制冷系统小时数
        int refrigerator = resp[22] << 8 + resp[23];
        //湿度系统小时数
        int humidity = resp[24] << 8 + resp[25];
        //空气系统小时数
        int air = resp[26] << 8 + resp[27];
        return new Integer[] { refrigerator, humidity, air };
    }

    //报警数据
    public static String[] alarmByte1 = new String[8];
    public static String[] alarmByte2 = new String[8];
    public static String[] alarmByte3 = new String[8];
    static {
        alarmByte1[0] = "高温报警        ";
        alarmByte1[1] = "低温报警        ";
        alarmByte1[2] = "传感器板故障报警";
        alarmByte1[3] = "高湿报警        ";
        alarmByte1[4] = "低湿报警        ";
        alarmByte1[5] = "气流丢失报警    ";
        alarmByte2[6] = "地板溢水报警    ";
        alarmByte2[7] = "未定义          ";
        alarmByte2[0] = "压缩机1低压报警 ";
        alarmByte2[1] = "压缩机1高压报警 ";
        alarmByte2[2] = "压缩机2高压报警 ";
        alarmByte2[3] = "制冷模式        ";
        alarmByte2[4] = "加热模式        ";
        alarmByte2[5] = "加湿模式        ";
        alarmByte2[6] = "除湿模式        ";
        alarmByte2[7] = "压缩机2低压报警 ";
        alarmByte3[0] = "制冷系统维护请求";
        alarmByte3[1] = "湿度系统维护请求";
        alarmByte3[2] = "空气系统维护请求";
        alarmByte3[3] = "加湿器低水位报警";
        alarmByte3[4] = "备用报警 1      ";
        alarmByte3[5] = "备用报警 3      ";
        alarmByte3[6] = "备用报警 2      ";
        alarmByte3[7] = "电源丢失报警    ";

    }

    public static List<String> getAlarmInQueryResp(byte[] resp) {
        List<String> ret = new LinkedList<String>();
        byte alarm1 = resp[8];
        byte alarm2 = resp[9];
        byte alarm3 = resp[10];
        for (int i = 0; i < 8; i++) {
            if (((alarm1 >> 7 - i) & 0x1) == 1) {
                ret.add(alarmByte1[i]);
            }
        }
        for (int i = 0; i < 8; i++) {
            if (((alarm2 >> 7 - i) & 0x1) == 1) {
                ret.add(alarmByte2[i]);
            }
        }
        for (int i = 0; i < 8; i++) {
            if (((alarm3 >> 7 - i) & 0x1) == 1) {
                ret.add(alarmByte3[i]);
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        int tSet = 17 + (86 & 0xF0) / 16;
        int hSet = 42 + (86 & 0xF) * 2;
        System.out.println(tSet + "\n" + hSet);
    }
}
